/***************************************************************************
 *   Copyright © 2010 by Nikolay Kornaev                                   *
 *   n.kornaev@gmail.com                                                   *
 *                                                                         *
 *                    This file is part of neuma.                          *
 *                                                                         *
 *   neuma is free software; you can redistribute it and/or modify it      *
 *   under the terms of the GNU General Public License as published by     *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   neuma is distributed in the hope that it will be useful,              *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, see <http://www.gnu.org/licenses/>   *
 ***************************************************************************/

#pragma once

#include <QtCore/QDir>
#include <QtGui/QFont>
#include <QtGui/QMainWindow>
#include <QtGui/QPainter>
#include <QtGui/QPrinter>
#include <QtGui/QSyntaxHighlighter>

#include "ui_MainWindow.h"

#include "DisplayWidget.h"
#include "Interface_SourceParser.h"
#include "SettingsHandler.h"
#include "SNPair.h"

class BlockNeuma;

namespace Neuma
{
  const QString N_DEFAULT_TYPE = "neumaplain";
/// на разных платформах высота шрифта высчитывается по-разному
  const qreal N_FONT_HEIGHT_MULTIPLICATOR =
#ifdef Q_OS_UNIX
      1.9; /// максимальная высота символа для Paraklht больше возвращаемой
#else
      1;
#endif

  class MainWindow: public QMainWindow, private Ui::MainWindow
  {
  Q_OBJECT

  public:
    MainWindow(const QString &fileName);

  private slots:
    void fillUi();
    void draw(QPrinter *printer);
    void processExport();

    void on_aAbout_triggered();
    void on_aAboutQt_triggered();
    //void on_aExit_triggered();
    void on_aNew_triggered();
    void on_aOpen_triggered();
    void on_aRefresh_triggered();
    bool on_aSave_triggered();
    bool on_aSaveAs_triggered();
    //void on_aExportPDF_triggered();
    void on_aExportSVG_triggered();
    void on_aPrint_triggered();
    void documentWasModified();

    void on_aSettings_triggered();

  private:
    void draw(QPainter &painter, bool isExport = false);
    void drawNeuma(QPainter &painter, const BlockNeuma &neuma, const QRectF &nRect);
    QFont nFont;
    QColor nColor, mColor;
    qreal syllabeCenterX(const QString &syllabe, const QFontMetricsF &fm, const bool byVocal);

    int exportImage(const QString & format);
    void writeSettings();

    bool maybeSave();
    void emptyLoad();
    void exitOnError(const QString &message);
    void loadFile(const QString &fileName);
    bool saveFile(const QString &fileName);
    void setCurrentFile(const QString &fileName);
    QString strippedName(const QString &fullFileName);
    QString constructFilter(bool onlyCurrent);

    void loadPlugins();
    QDir pluginsDir;
    QMap<QString, SourceParser *> plugins; // QMap<supportedExtension, plugin>
    SourceParser *currentSP;

    QString curFile;

    QString path;
    SettingsHandler *set;
    NeumasData *data;
    QPainter painter;
    QPrinter printer;

    DisplayWidget widget;
    QSyntaxHighlighter *highlighter;

  protected:
    void closeEvent(QCloseEvent *event);
  };
}
